<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use app\common\model\SearchLog;
use fast\Payment;
class Adviser extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {

            $keywords = input('keywords','');
            $map['status'] = 'normal';
            if($keywords){
                $searchlog = SearchLog::get(['name'=>$keywords]);
                if(empty($searchlog)){
                    $searchlog = new Searchlog();
                    $searchlog->name = $keywords;
                    $searchlog->nums = 1;
                    
                }else{
                    $searchlog->nums = $searchlog['nums'] + 1;
                }
                $searchlog->save();
                $map['title|content'] = ['like','%'.$keywords.'%'];
            }
            $data = model('Adviser')->with('categories')->where($map)->order('year desc,month desc')->select();
            $list=[];
            if(!empty($data)){
                $list1 = [];
                foreach ($data as $key => $value) {
                    $value['nums'] = Db::name('AdviserOrder')->where('adviser_id',$value['id'])->where('pay_status',1)->count('id');
                    $list1[$value['year'].'-'.$value['month']][] = $value;
                }
                foreach ($list1 as $key => $value) {
                    $title = str_replace('-','年',$key).'月实战活动';
                    $list[] = ['title'=>$title,'data'=>$value,'id'=>$key];
                }
            }        
        /**
         * 我参与的
         */
        $cate = Db::name('Category')->where('pid',7)->select();
        $this->assign('list', $list);
        $this->assign('cate', $cate);
        return $this->view->fetch();
    }
    
    //谋士详情
    public function detail(){
        $id = input('id');
        $adviser = Db::name('Adviser')->where('id',$id)->find();
        $arttype = Db::name('AdviserArticleType')->where('adviser_id',$id)->order('weigh desc')->select();
        $article = Db::name('AdviserArticle')->where('adviser_id',$id)->order('weigh desc')->select();
        $order = Db::name('AdviserOrder')->where('adviser_id',$id)->where('user_id',$this->auth->id)->where('pay_status',1)->find();
        if($order){
            $adviser['is_canjia'] = 1;
        }else{
            $adviser['is_canjia'] = 0;
        }

        $adviser['is_edit'] = in_array($this->auth->id,explode(',',$adviser['user_id']))?1:0;

        $log = Db::name('AdviserLog')->alias('l')->join('wd_adviser d','d.id = l.adviser_id')->where('l.adviser_id',$id)->order('id desc')->field('d.title,l.*')->select();

        foreach ($log as $key => &$value) {
            $config = Db::name('AdviserConfig')->where('adviser_id',$value['adviser_id'])->column('title,type,content,tip','name');
            $value['data'] = [];
            if(!empty($value['content'])){
                foreach(json_decode($value['content'],true) as $k => $v){
                    $data['title']=$config[$k]['title'].($config[$k]['tip']?'['.$config[$k]['tip'].']':'');
                    if($config[$k]['type'] == 'select'){
                        $data['value']=json_decode($config[$k]['content'],true)[$v];
                    }else if($config[$k]['type'] =='selects'){
                        $con = json_decode($config[$k]['content'],true);
                        foreach ($v as  $val) {
                            $data['value'].=$con[$val].' ';
                        }
                    }else{
                        $data['value']=$v;
                    }
                    $value['data'][] = $data;
                }
            }
            $value['user'] = Db::name('User')->where('id',$value['user_id'])->field('id,nickname,avatar')->find();
        }



        $this->assign('article', $article);
        $this->assign('arttype', $arttype);
        $this->assign('adviser', $adviser);
        $this->assign('log', $log);
        return $this->view->fetch();
    }
    
    //实战手册
    public function guide(){
        $id = input('id');
        $adviser = Db::name('Adviser')->where('id',$id)->find();
        if($this->request->isAjax()){
            //查看实战，报名在内
            $order = Db::name('AdviserOrder')->where('adviser_id',$id)->where('user_id',$this->auth->id)->where('pay_status',1)->find();
            if($order){
                $this->success('已报名');
            }else{
                $adv = Db::name('Adviser')->where('id',$id)->find();
                if($adv['end_time']>time()){
                    $this->error();
                }else{
                    $this->success('已过期');
                }
                
            }
        }
        
        $user = Db::name('User')->where('id','in',$adviser['user_id'])->select();
        $guide = Db::name('AdviserGuide')->where('adviser_id',$id)->where('is_audit',1)->order('weigh desc')->select();
        $this->assign('guide', $guide);
        $this->assign('user', $user);
        $this->assign('adviser', $adviser);
        return $this->view->fetch();
    }
    
    //使用指南
    public function tutorial(){
        $info = Db::name('Category')->where('id',11)->find();
        $this->assign('info', $info);
        return $this->view->fetch();
    }
    //谋士公约
    public function convention(){
        $info = Db::name('Category')->where('id',12)->find();
        $this->assign('info', $info);
        return $this->view->fetch();
    }
    public function edit($id=null){
        if($this->request->isPost()){
            $data = $this->request->post('row/a');
            $data['user_id'] = $this->auth->id;
            $data['adviser_id'] = $id;
            $guide =model('Guide')->create($data,true);
            if($guide){
                $this->success('');
            }else{
                $this->error('上传失败');
            }
        }
        return $this->view->fetch();
    }
   
    public function author($id=null){
        $guide = Db::name('AdviserGuide')->alias('g')->join('wd_user u','u.id = g.user_id')->where('g.adviser_id',$id)->where('g.is_audit',1)->order('weigh desc')->select();
        $adviser = Db::name('Adviser')->where('id',$id)->find();
        $this->assign('adviser', $adviser);
        $this->assign('list',$guide);
        return $this->view->fetch();
    }
     //谋士好事
    public function lists($id=null){
        if($this->request->isAjax()){
            $params = $this->request->post();
            $page = $params['page']?:1;
            $map['l.status'] = 'normal';
            if($params['adviser_id']){
                $map['l.adviser_id'] = $params['adviser_id'];
            }
            $list = Db::name('AdviserLog')->alias('l')->join('wd_adviser d','d.id = l.adviser_id')->where($map)->page($page,10)->order('id desc')->field('d.title,l.*')->select();

                foreach ($list as $key => &$value) {
                    $config = Db::name('AdviserConfig')->where('adviser_id',$value['adviser_id'])->column('title,type,content','name');
                    $data ='';
                    if(!empty($value['content'])){
                        foreach(json_decode($value['content'],true) as $k => $v){
                            if(!empty($v)){
                                $data.=$config[$k]['title'];
                                if($config[$k]['type'] == 'select'){

                                    $data.=json_decode($config[$k]['content'],true)[$v];
                                }else if($config[$k]['type'] =='selects'){
                                    $con = json_decode($config[$k]['content'],true);
                                    
                                    foreach ($v as  $val) {
                                        $data.=$con[$val].';';
                                    }

                                }else{
                                    $data.=$v;
                                }
                                $data.='，';
                            }
                        }
                    }
                    $value['data'] = $data;
                    $value['user'] = Db::name('User')->where('id',$value['user_id'])->field('id,nickname,avatar')->find();
                }
            
            return json(['code'=>200,'data'=>$list,'msg'=>'success']);
        }
        $title = '';
        if($id){
            $title = Db::name('Adviser')->where('id',$id)->value('title');

        }
        $this->assign('id', $id?$id:0);
        $this->assign('title', $title);
        return $this->view->fetch();
    }
   
    //谋士日志编辑
    public function logedit($id=null){
       if($this->request->isPost()){
           $params = $this->request->post('row/a');
           $data['content'] = json_encode($params);
           $data['user_id'] = $this->auth->id;
           $data['adviser_id'] = $id;
           model('AdviserLog')->create($data,true);
           $this->success();
       }
       $configModel = model('AdviserConfig');
       $config = $configModel::where('adviser_id',$id)->order('weigh desc')->select();
       $this->view->assign('list', $config);
       return $this->view->fetch();
    }
    public function baoming($id=null){
        if($this->request->isAjax()){
            $params = $this->request->post();
            $adviser = Db::name('Adviser')->where('id',$params['id'])->find();
            if($params['pay'] == 'wechat'){
                $result = Payment::wechat($adviser['id'],'adviser',$adviser['price'],$this->auth->id,json_encode($params,1));
            }
            if($params['pay'] == 'alipay'){
                $result = Payment::alipay($adviser['id'],'adviser',$adviser['price'],$this->auth->id,json_encode($params,1));
            }
            if(!$result){
                $this->error('创建支付订单失败');
            }else{
                $this->success('','',$result);
            }
        }
        $adviser = Db::name('Adviser')->where('id',$id)->find();
        $user = Db::name('User')->where('id',$this->auth->id)->find();
        $this->assign('adviser', $adviser);
        $this->assign('user', $user);
        return $this->view->fetch();
    }
}